﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using PRET.ReportDataDefine.UIData;
using PRET.ReportControlLib.ServiceReference;

namespace PRET.ReportControlLib.ReportView
{
    public partial class MeterReadingReportView : UserControl
    {
        private PRETWebServiceClient adapter = null;

        public MeterReadingReportView()
        {
            InitializeComponent();
            adapter = new PRETWebServiceClient();
            this.setMeterTimePanel.showReportEvent += setMeterTimePanel_showReportEvent;
        }

        void setMeterTimePanel_showReportEvent(object sender, Controls.ShowMeterReportViewEventArgs args)
        {
            try
            {
                this.setMeterTimePanel.EnableControls(false);

                if (this.adapter == null)
                {
                    MessageBox.Show("MeterReadingReportView 没有初始化Server Adapter!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                this.meterReadingReportViewer.LocalReport.DataSources.Clear(); //clear report
                // 从后台获取特定时间段内的电表数据
                MeterReadingReport meterRead = this.adapter.GetMeterReadingReport(args.StartTime, args.EndTime, args.PeakUP, args.GenUP, args.LowUP);
                if (meterRead != null)
                {
                    Microsoft.Reporting.WinForms.ReportDataSource summaryDataset = new Microsoft.Reporting.WinForms.ReportDataSource("MeterReadingSummaryDS"); // set the datasource
                    this.meterReadingReportViewer.LocalReport.DataSources.Add(summaryDataset);
                    List<MeterReadingSummary> list = new List<MeterReadingSummary> { meterRead.Summary };
                    summaryDataset.Value = list;

                    Microsoft.Reporting.WinForms.ReportDataSource detailsDataset = new Microsoft.Reporting.WinForms.ReportDataSource("MeterReadingDetailInfoDS");
                    this.meterReadingReportViewer.LocalReport.DataSources.Add(detailsDataset);
                    detailsDataset.Value = meterRead.DataList;
                }
                this.meterReadingReportViewer.LocalReport.Refresh();
                this.meterReadingReportViewer.RefreshReport();
            }
            catch (Exception ex)
            {
                string msg = string.Format("MeterReadingReportView.setMeterTimePanel_showReportEvent() Failed: {0}.\r\nDetail: {1}", ex.Message, ex.StackTrace);
                MessageBox.Show(msg, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            { this.setMeterTimePanel.EnableControls(true); }
        }

        public void SetUnitPrice()
        {
            float[] prices = this.adapter.GetMeterUnitPrice();
            if (prices != null && prices.Length == 3)
            {
                this.setMeterTimePanel.PeakUP = prices[0];
                this.setMeterTimePanel.GenUP = prices[1];
                this.setMeterTimePanel.LowUP = prices[2];
            }
        }

        //public void ShowReportView()
        //{
        //    if (this.adapter == null)
        //    {
        //        MessageBox.Show("MeterReadingReportView 没有初始化Server Adapter!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        //        return;
        //    }

        //    this.meterReadingReportViewer.LocalReport.DataSources.Clear(); //clear report
        //    // 从后台获取特定时间段内的电表数据
        //    float peak = this.setMeterTimePanel.PeakUP;
        //    float gen = this.setMeterTimePanel.GenUP;
        //    float low = this.setMeterTimePanel.LowUP;
        //    MeterReadingReport meterRead = this.adapter.GetMeterReadingReport(setMeterTimePanel.getStartTime, setMeterTimePanel.getEndTime, peak, gen, low);
        //    if (meterRead != null)
        //    {
        //        Microsoft.Reporting.WinForms.ReportDataSource summaryDataset = new Microsoft.Reporting.WinForms.ReportDataSource("MeterReadingSummaryDS"); // set the datasource
        //        this.meterReadingReportViewer.LocalReport.DataSources.Add(summaryDataset);
        //        List<MeterReadingSummary> list = new List<MeterReadingSummary> { meterRead.Summary };
        //        summaryDataset.Value = list;

        //        Microsoft.Reporting.WinForms.ReportDataSource detailsDataset = new Microsoft.Reporting.WinForms.ReportDataSource("MeterReadingDetailInfoDS");
        //        this.meterReadingReportViewer.LocalReport.DataSources.Add(detailsDataset);
        //        detailsDataset.Value = meterRead.DataList;
        //    }
        //    this.meterReadingReportViewer.LocalReport.Refresh();
        //    this.meterReadingReportViewer.RefreshReport();
        //}
    }
}
